package com.ems.sys.controller;
import com.ems.entity.Result;
import com.ems.entity.StatusCode;
import com.ems.sys.pojo.Salary;
import com.ems.sys.service.SalaryService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sys/salary")
//@CrossOrigin
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    /***
     * Salary分页条件搜索实现
     * @param salary
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Salary salary, @PathVariable  int page, @PathVariable  int size){
        //调用SalaryService实现分页条件查询Salary
        PageInfo<Salary> pageInfo = salaryService.findPage(salary, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Salary分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SalaryService实现分页查询Salary
        PageInfo<Salary> pageInfo = salaryService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param salary
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Salary>> findList(@RequestBody(required = false)  Salary salary){
        //调用SalaryService实现条件查询Salary
        List<Salary> list = salaryService.findList(salary);
        return new Result<List<Salary>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    @Secured({"ROLE_ADMIN"})//基于角色可以访问
    public Result delete(@PathVariable String id){
        //调用SalaryService实现根据主键删除
        salaryService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Salary数据
     * @param salary
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    @Secured({"ROLE_ADMIN"})//基于角色可以访问
    public Result update(@RequestBody  Salary salary,@PathVariable String id){
        //设置主键值
        salary.seteId(id);
        //调用SalaryService实现修改Salary
        salaryService.update(salary);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Salary数据
     * @param salary
     * @return
     */
    @PostMapping
        @Secured({"ROLE_ADMIN"})//基于角色可以访问
    public Result add(@RequestBody   Salary salary){
        //调用SalaryService实现添加Salary
        salaryService.add(salary);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Salary数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Salary> findById(@PathVariable String id){
        //调用SalaryService实现根据主键查询Salary
        Salary salary = salaryService.findById(id);
        return new Result<Salary>(true,StatusCode.OK,"查询成功",salary);
    }

    /***
     * 查询Salary全部数据
     * @return
     */
    @GetMapping
    public Result<List<Salary>> findAll(){
        //调用SalaryService实现查询所有Salary
        List<Salary> list = salaryService.findAll();
        return new Result<List<Salary>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
